import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class VideoPlayerSingleton {
  // 私有构造函数，确保只有一个实例
  VideoPlayerSingleton._privateConstructor();

  // 单例实例
  static final VideoPlayerSingleton instance =
      VideoPlayerSingleton._privateConstructor();

  // VideoPlayerController 实例
  VideoPlayerController? _controller;

  // 获取控制器
  VideoPlayerController? get controller => _controller;
  String longVideo = "https://media.w3.org/2010/05/sintel/trailer.mp4";

  // 声明一个变量来存储 overlayEntry
  OverlayEntry? overlayEntry;

  // 初始化控制器
  Future<void> initializeController(String? videoUrl,
      {void Function()? callback}) async {
    _controller ??=
        VideoPlayerController.networkUrl(Uri.parse(videoUrl ?? longVideo))
          ..initialize().then((value) {
            _controller?.play();

            if (callback != null) {
              callback();
            }
          });
  }

  // 销毁控制器
  void disposeController() {
    _controller?.pause();
    _controller?.dispose();
    _controller = null;
  }

  // 使用 Overlay 展示悬浮视频窗口
  // void showFloatingVideo(BuildContext context) {
  //   final overlay = Overlay.of(context);

  //   // 先声明并插入 overlayEntry
  //   overlayEntry = OverlayEntry(
  //     builder: (context) => Positioned(
  //       top: 100,
  //       left: 50,
  //       right: 50,
  //       child: Material(
  //         color: Colors.transparent,
  //         child: GestureDetector(
  //           onTap: () {
  //             removeOverlayEntry();
  //           },
  //           child: Container(
  //             height: 200,
  //             width: 300,
  //             color: Colors.black,
  //             child: _controller!.value.isInitialized
  //                 ? AspectRatio(
  //                     aspectRatio: _controller!.value.aspectRatio,
  //                     child: VideoPlayer(_controller!),
  //                   )
  //                 : const Center(child: CircularProgressIndicator()), // 显示加载指示器
  //           ),
  //         ),
  //       ),
  //     ),
  //   );

  //   // 插入到 overlay
  //   overlay.insert(overlayEntry!);
  // }

  void showFloatingVideo(BuildContext context) {
    final overlay = Overlay.of(context);

    // 先声明并插入 overlayEntry
    overlayEntry = OverlayEntry(
      builder: (context) {
        // 记录悬浮窗口的位置
        double topPosition = 100;
        double leftPosition = 50;

        return Positioned(
          top: topPosition,
          left: leftPosition,
          child: Material(
            color: Colors.transparent,
            child: GestureDetector(
              onPanUpdate: (details) {
                // 更新窗口位置，实现拖动效果
                // 确保 primaryDelta 不为 null
                if (details.primaryDelta != null) {
                  topPosition += details.primaryDelta!;
                  leftPosition += details.primaryDelta!;
                }
              },
              child: Stack(
                children: [
                  // 这是视频播放器区域
                  Container(
                    height: 200,
                    width: 300,
                    color: Colors.black,
                    child: _controller!.value.isInitialized
                        ? AspectRatio(
                            aspectRatio: _controller!.value.aspectRatio,
                            child: VideoPlayer(_controller!),
                          )
                        : const Center(child: CircularProgressIndicator()),
                  ),
                  // 右上角的关闭按钮
                  Positioned(
                    top: 10,
                    right: 10,
                    child: IconButton(
                      icon: const Icon(
                        Icons.close,
                        color: Colors.white,
                      ),
                      onPressed: () {
                        removeOverlayEntry(); // 点击关闭按钮移除悬浮窗口
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );

    // 插入到 overlay
    overlay.insert(overlayEntry!);
  }

  void removeOverlayEntry() {
    disposeController();
    // 点击悬浮窗口，移除悬浮视频
    print("点击悬浮窗口，移除悬浮视频");
    overlayEntry?.remove();
    overlayEntry = null; // 清空悬浮窗口
  }
}
